package org.deri.feather.common;

public class LiteralElement extends Element {

	private String language;
	private String type;
	private String value;
	
	public LiteralElement(String stringValue) {
		super(stringValue, Element.LITERAL_TYPE);
		this.language = extractLanguage(stringValue);
		this.type = extractType(stringValue);
		this.value = extractValue(stringValue);
	}
	
	public LiteralElement(String value, String type, String language) {
		super("", Element.LITERAL_TYPE);
		
		String stringValue = "\"" + value + "\"";
		if (!language.equals(""))
			stringValue += "@" + language;
		
		if(!type.equals(""))
			stringValue += "^^<" + type + ">";
		
		this.language = language;
		this.type = type;
		this.value = value;
		this.stringValue = stringValue;
	}
	
	public String getLanguage() {
		return this.language;
	}
	
	public String getType() {
		return this.type;
	}
	
	public String getValue() {
		return this.value;
	}
	
	private String extractLanguage(String text) {
		String language = "";
		
		if(text.contains("@")) {
			text = text.substring(text.indexOf("@")+1);
			if(text.contains("^^"))
				language = text.substring(0, text.indexOf("^^"));
			else
				language = text;
		}
		return language;
	}
	
	private String extractType(String text) {
		String type = "";
		
		if(text.contains("^^")) {
			if (text.contains("<"))
				type = text.substring(text.indexOf("^^<")+3, text.lastIndexOf(">"));
			else
				type = text.substring(text.indexOf("^^")+2);
		}
		
		return type;
	}
	
	private String extractValue(String text) {
		String value = text;
		
		if (value.contains("@"))
			value = value.substring(0, value.indexOf("@"));
		
		if (value.contains("^^"))
			value = value.substring(0, value.indexOf("^"));
		
		value = value.replace("\"", "");
		return value;
	}
}
